package com.hs.components.actions;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.aq.model.AqFile;
import com.hs.Environment;
import com.hs.common.page.PageParam;
import com.hs.common.page.PageUtil;
import com.hs.components.service.IFileManagerService;
import com.hs.components.service.imp.FileManagerService;
import com.hs.core.action.AbstractAction;

public class FileUploadAction extends AbstractAction {
	private static final long serialVersionUID = 572146812454l ;
    private static final int BUFFER_SIZE = 16 * 1024 ;
   
    protected Collection availableItems;
    private List<File> uploads = new ArrayList<File>();
    private List<String> uploadFileNames = new ArrayList<String>();
    private List<String> uploadContentTypes = new ArrayList<String>();
    private IFileManagerService uploadService;
    protected String pageStr; 
    protected PageUtil pageUtil;  
    protected Long fileid;
    protected AqFile aqfile;

    public AqFile getAqfile() {
		return aqfile;
	}
	public void setAqfile(AqFile aqfile) {
		this.aqfile = aqfile;
	}
	public List <File> getUpload()  {
        return this.uploads;
   } 
     public void setUpload(List<File> uploads)  {
        this.uploads = uploads;
   } 

     public List <String> getUploadFileName()  {
        return this.uploadFileNames;
   } 
     public void setUploadFileName(List <String> uploadFileNames)  {
        this.uploadFileNames = uploadFileNames;
   } 

     public List<String> getUploadContentType()  {
        return this.uploadContentTypes;
   } 
     public void setUploadContentType(List<String> contentTypes)  {
        this.uploadContentTypes = contentTypes;
   } 
   
    private static void copy(File src, File dst)  {
        try  {
           InputStream in = null ;
           OutputStream out = null ;
            try  {                
               in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
               out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
                byte [] buffer = new byte [BUFFER_SIZE];
                while (in.read(buffer) > 0 )  {
                   out.write(buffer);
               } 
            } finally  {
                if ( null != in)  {
                   in.close();
               } 
                 if ( null != out)  {
                   out.close();
               } 
           } 
        } catch (Exception e)  {
           e.printStackTrace();
       } 
   } 
    public String list() throws Exception { 
    	PageParam pageParam = new PageParam(this.getHttpServletRequest(),"listNews.action");
		String hql = " from AqFile where 1=1";
		pageParam.setSearchSql(hql);
		pageUtil = uploadService.getFileList(pageParam);
		availableItems = pageUtil.getPageList();
		pageStr = pageUtil.getPageStr();
		this.getHttpServletRequest().setAttribute("dataList", availableItems);
        return SUCCESS;         
    }
    private static String getExtention(String fileName)  {
        int pos = fileName.lastIndexOf( "." );
        return fileName.substring(pos);
    }  
    public String save(){
    	 String extention = "";
	     for(int i = 0;i < uploadFileNames.size();i++){
	    	 extention = getExtention(uploadFileNames.get(i));
	    	 String imageFileName = new Date().getTime() + extention ;
	    	 String fileFullPath = Environment.getValue(Environment.PROP_FILE_DIR_FULL_PATH) + "/" + imageFileName;
	    	 File imageFile = new File(fileFullPath);
	    	 copy(uploads.get(i), imageFile);
	    	 aqfile = new AqFile();
	    	 aqfile.setFileName(uploadFileNames.get(i));
	    	 aqfile.setFileType(extention);
	    	 aqfile.setFullPath(fileFullPath);
	    	 aqfile.setUploadDate(new Date());
	    	 //aqfile.setUploadUser(); 
	    	 uploadService.saveObject(aqfile);
	     } 
        return SUCCESS;
   }
	public IFileManagerService getUploadService() {
		return uploadService;
	}
	public void setUploadService(IFileManagerService uploadService) {
		this.uploadService = uploadService;
	}
	public String getPageStr() {
		return pageStr;
	}
	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	public Long getFileid() {
		return fileid;
	}
	public void setFileid(Long fileid) {
		this.fileid = fileid;
	}
	public Collection getAvailableItems() {
		return availableItems;
	}
	public void setAvailableItems(Collection availableItems) {
		this.availableItems = availableItems;
	} 

}
